{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Find Places Tutorial\n",
    "This is a completed solution for the [Find Places](https://developers.arcgis.com/labs/python/find-places/) ArcGIS tutorial. \n",
    "\n",
    "[ArcGIS tutorials](https://developers.arcgis.com/labs/) are short guides demonstrating the three phases of building geospatial apps: Data, Design, Develop."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1.** Model a GIS portal and import the geocode function from the ArcGIS API for Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis.gis import GIS\n",
    "from arcgis.geocoding import geocode"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2.** Create an anonymous connection to ArcGIS Online. Since the results are not stored, you do not need credentials to call the geocoding service"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "gis = GIS()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**3.** Pass the relevant parameters into the geocode function and get the results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "geocode_fs = geocode(address=None,\n",
    "                     location=[-118.71511, 34.09042],\n",
    "                     category=\"Coffee shop\",\n",
    "                     out_fields=\"Place_addr, PlaceName\",\n",
    "                     max_locations=25,\n",
    "                     as_featureset=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4.** Convert the results to a dataframe and show the first two locations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>OBJECTID</th>\n",
       "      <th>PlaceName</th>\n",
       "      <th>Place_addr</th>\n",
       "      <th>SHAPE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Starbucks</td>\n",
       "      <td>26531 Agoura Rd, Calabasas, California, 91302</td>\n",
       "      <td>{\"x\": -118.70110999999997, \"y\": 34.14358000000...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Starbucks</td>\n",
       "      <td>26521 Agoura Rd, Calabasas, California, 91302</td>\n",
       "      <td>{\"x\": -118.70023995051699, \"y\": 34.14425002668...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Starbucks</td>\n",
       "      <td>23755 Malibu Rd, Malibu, California, 90265</td>\n",
       "      <td>{\"x\": -118.69344999999998, \"y\": 34.03431000000...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>The Coffee Bean &amp; Tea Leaf</td>\n",
       "      <td>3835 Cross Creek Rd, Malibu, California, 90265</td>\n",
       "      <td>{\"x\": -118.68542004616472, \"y\": 34.03591994503...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Starbucks</td>\n",
       "      <td>3900 Cross Creek Rd, Malibu, California, 90265</td>\n",
       "      <td>{\"x\": -118.68528998441938, \"y\": 34.03535004385...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   OBJECTID                   PlaceName  \\\n",
       "0         1                   Starbucks   \n",
       "1         2                   Starbucks   \n",
       "2         3                   Starbucks   \n",
       "3         4  The Coffee Bean & Tea Leaf   \n",
       "4         5                   Starbucks   \n",
       "\n",
       "                                       Place_addr  \\\n",
       "0   26531 Agoura Rd, Calabasas, California, 91302   \n",
       "1   26521 Agoura Rd, Calabasas, California, 91302   \n",
       "2      23755 Malibu Rd, Malibu, California, 90265   \n",
       "3  3835 Cross Creek Rd, Malibu, California, 90265   \n",
       "4  3900 Cross Creek Rd, Malibu, California, 90265   \n",
       "\n",
       "                                               SHAPE  \n",
       "0  {\"x\": -118.70110999999997, \"y\": 34.14358000000...  \n",
       "1  {\"x\": -118.70023995051699, \"y\": 34.14425002668...  \n",
       "2  {\"x\": -118.69344999999998, \"y\": 34.03431000000...  \n",
       "3  {\"x\": -118.68542004616472, \"y\": 34.03591994503...  \n",
       "4  {\"x\": -118.68528998441938, \"y\": 34.03535004385...  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# use `sdf` not `df` to convert the FeatureSet into a Pandas Dataframe\n",
    "g_df = geocode_fs.sdf\n",
    "g_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**5.** Create a map to display the results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8d0a6fb1716345cd9d6e549e49741fb1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(layout=Layout(height='400px', width='100%'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"map-static-img-preview-a447e6c6-dfa1-4081-ae5c-e0933bce7b26\"><img src=\"\"></img></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "g_map = gis.map()\n",
    "g_map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "g_map.center = [34.09042, -118.71511]\n",
    "g_map.zoom = 11"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**6.** Once the map has loaded, draw the results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "g_map.draw(geocode_fs)"
   ]
  }
 ],
 "metadata": {
  "esriNotebookRuntime": {
   "notebookRuntimeName": "ArcGIS Notebook Python 3 Standard",
   "notebookRuntimeVersion": "9.0"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
